﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace CNKI.TPI.Web.UI.Controllers.UserCenter
{
    public class UserCenterController : BaseController
    {
        IUserCenterService centerService = null;

        public UserCenterController(IUserCenterService centerService)
        {
            this.centerService = generator.CreateInterfaceProxyWithTarget<IUserCenterService>(centerService, new IInterceptor[] { new LogInterceptor() });
        }
        //
        // GET: /UserCenter/

        public ActionResult UserCenterIndex(string controllstr, string actionstr)
        {
            if (string.IsNullOrEmpty(actionstr))
            {
                actionstr = "SearchHistory";
            }
            ViewBag.UrlStr = Url.Action(actionstr, controllstr);
            return View();
        }
        public ActionResult SearchHistory()
        {
            return View();
        }

        [ValidateInput(false)]
        public ActionResult SearchHistorySearch(string dbname, DateTime? startdate, DateTime? enddate, int pageIndexNow)
        {
            int pageSize = 10;//每页显示记录数
            int count = 0;//总条数

            DataTable dt = centerService.GetSearchPage(dbname, startdate, enddate, pageIndexNow, pageSize, out count);
            pagelistGroup(dt, pageIndexNow, pageSize, count);
            return PartialView("SearchHistoryResult");
        }
        public ActionResult ViewHistory()
        {
            return View();
        }

        [ValidateInput(false)]
        public ActionResult ViewHistorySearch(string dbname, string title, DateTime? startdate, DateTime? enddate, int pageIndexNow)
        {
            int pageSize = 10;//每页显示记录数
            int count = 0;//总条数

            DataTable dt = centerService.GetViewPage(dbname, title, startdate, enddate, pageIndexNow, pageSize, out count);
            pagelistGroup(dt, pageIndexNow, pageSize, count);
            return PartialView("ViewHistoryResult");
        }
        public ActionResult DownHistory()
        {
            return View();
        }

        [ValidateInput(false)]
        public ActionResult DownHistorySearch(string dbname, string title, DateTime? startdate, DateTime? enddate, int pageIndexNow)
        {
            int pageSize = 10;//每页显示记录数
            int count = 0;//总条数

            DataTable dt = centerService.GetDownPage(dbname, title, startdate, enddate, pageIndexNow, pageSize, out count);
            pagelistGroup(dt, pageIndexNow, pageSize, count);
            return PartialView("DownHistoryResult");
        }


        public ActionResult ManagerCategoryIndex()
        {
            return View();
        }

        public ActionResult ManagerCategoryIndexSearch(int pageIndexNow)
        {
            int favcount = 0;
            int pageSize = 10;//每页显示记录数
            int count = 0;//总条数
            //获取分类列表
            DataTable dt = centerService.GetFavoriteCategory(pageIndexNow, pageSize, out count);
            ViewBag.FavList = dt;

            if (dt != null && dt.Rows.Count > 0)
            {
                favcount = dt.Rows.Count;
            }
            ViewBag.FavCount = favcount;
            pagelistGroup(dt, pageIndexNow, pageSize, count);
            return View("ManagerCategoryIndexResult");
        }

        public JsonResult GetFavList()
        {
            DataTable dt = centerService.GetFavoriteCategory();
            //获取字段
            var res = new JsonResult();
            res.Data = SerializeHelper.Serialize(dt);
            res.MaxJsonLength = 102400000;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;
        }
        public bool SaveFav(string favname,int favstatus,int id)
        {

            if (id == 0)//新增
            {
                return centerService.InsertFavoriteCategory(favname, favstatus);
            }
            else
            { //修改

                return centerService.UpdateFavoriteCategoryName(favname, id);
            }
        }
        public bool DelFav(int id)
        {
            return centerService.DelFavoriteCategory(id);
        }

        public bool UpdateFavSort(IList<Dictionary<string,string>> list)
        {
            foreach (Dictionary<string, string> df in list)
            {

                centerService.UpdateFavoriteCategorySort(int.Parse(df["SortNo"].ToString()), int.Parse(df["FavoriteCategoryID"].ToString()));
            }

            return true;
        }

        private void pagelistGroup(DataTable dt, int pageIndexNow, int pageSize, int count)
        {
            int sumPage = 0;//一共多少页
            int disPgae = 8;  //默认显示页数，当页数大于该数值时，按规定格式显示
            if (dt != null && dt.Rows.Count > 0)
            {
                ViewBag.SearchList = dt;
            }
            sumPage = count / pageSize; //一共多少页
            if (count % pageSize != 0)
            {
                sumPage = sumPage + 1;
            }
            ColumnAliasHelper com = new ColumnAliasHelper();
            ViewBag.TotalPage = sumPage;
            ViewBag.PageList = com.GetPageList(pageIndexNow, sumPage, disPgae);
            ViewBag.PageNow = pageIndexNow;//当前页
            ViewBag.PageSize = pageSize;
            ViewBag.Count = count;
        }
        public bool IsExistFavName(int id,string cateName)
        {
           int flage= centerService.IsExistFavName(cateName);
            if(flage==0)//不存在
            {
                return false;
            }
            else //存在
            {
                if (id != 0 && id == flage)//修改模式，排除修改记录本身
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// 新增收藏记录
        /// </summary>
        /// <param name="catename"></param>
        /// <param name="catestatus"></param>
        /// <param name="sortno"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public bool InsertFavoriteFolder(int favCateID, string dbCode, string dbName, string recid, string recTitle)
        {
            return centerService.InsertFavoriteFolder(favCateID,dbCode,dbName,recid,recTitle);
        }
        /// <summary>
        /// 收藏列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult FavFolder()
        {
            //获取分类列表
            DataTable dt = centerService.GetFavoriteCategory();
            ViewBag.FavCateList = dt;
            return View();
        }

        [ValidateInput(false)]
        /// <summary>
        /// 收藏列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult FavFolderResult(int catid, string dbname, string title, DateTime? startDate, DateTime? endDate, int pageIndexNow)
        {
            int pageSize = 10;//每页显示记录数
            int count = 0;//总条数
            //获取收藏记录
            DataTable dt = centerService.GetFavoriteFolder(catid, dbname, title, startDate, endDate, pageIndexNow, pageSize, out count);
            pagelistGroup(dt, pageIndexNow, pageSize, count);
            return View();
        }
        public bool DelFolder(string id)
        {
            return centerService.DelFavoriteFolder(id);
        }
        public bool UpdateFolderCate(int cateid, int floderid)
        {
            return centerService.UpdateFavoriteFolder(cateid,floderid);
        }
        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="recid"></param>
        /// <returns></returns>
        public bool CancelFavByRecID(int recid)
        {
            return centerService.UpdateFavStatusByRecID(recid);
        }

    }
}
